**Code to generate the results in Figure 2

*PNAD 1988 (Brazilian Household Survey of 1988)
clear
cd "/Users/lucienetorresdemellopereira/Documents/EPGE_Mac/Doutorado/PNAD"
use Pnad_88.dta

*Generating the variables
gen urban = [v0003==1 | v0003==3]
gen rural = [v0003==5 | v0003==7]
rename v0805 age  
rename v9991 weight_p 
gen gender_man = [v0303==1]
gen gender_woman = [v0303==3]
rename v0010 state 

gen educ_child1 = [v0318==1]
gen educ_child2 = [v0318>=2 & v0318<=5]
gen educ_child3 = [v0318>=6 & v0318<=9]
gen educ_child4 = [v0318==10]
gen educ_child5 = [v0318==11]
gen educ_child = 1*educ_child1+2*educ_child2+3*educ_child3+4*educ_child4+5*educ_child5
replace educ_child=. if educ_child==0

gen educ_dad1 = [v2301==1 | v2301==2]
gen educ_dad2 = [v2301==3 | v2301==4]
gen educ_dad3 = [v2301==5]
gen educ_dad4 = [v2301==6]
gen educ_dad5 = [v2301==7]
gen educ_dad = 1*educ_dad1+2*educ_dad2+3*educ_dad3+4*educ_dad4+5*educ_dad5
replace educ_dad=. if educ_dad==0

*v0602: variable of total income of the individual in urban areas
gen income = v0602 if (v0602>0 & age>=25 & urban==1)
pctile pct_RJ = income if (state>=11 & state<=14) [w=weight_p], nq(20)
* 35 Percentile - RJ area: 30000


*Generating the education transition matrices (father and child)
*RJ: 30000
tab educ_dad educ_child [iweight=weight_p] if (age>=25 & age<=109 & urban==1 & state>=11 & state<=14 & income>0 & income<=30000)
tab educ_dad educ_child [iweight=weight_p] if (age>=25 & age<=109 & urban==1 & state>=11 & state<=14 & income>30000)

*Rural Brazil
tab educ_dad educ_child [iweight=weight_p] if (age>=25 & age<=109 & rural==1)